import { PageTool } from "../../../../common/global/PageTool";
import { Cache } from "../../../cache/Cache";
import { RechargeGiftBagItem } from "./RechargeGiftBagItem";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GiftBagConfig } from "../../../resource/GiftBagConfig";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { OpenRechargeGiftBagConfig } from "../../../resource/OpenRechargeGiftBagConfig";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { NoviceGiftItemRenderer } from "../../novice/NoviceGiftItemRenderer";
import { ScrollPolicy } from "../../../../../flashx/textLayout/container/ScrollPolicy";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { RechargeProgressBar } from "./RechargeProgressBar";
import { ResFileConst } from "../../../resource/ResFileConst";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { RemainTimeUtil } from "../../shopMall/RemainTimeUtil";
import { RemainTimeTool } from "../../award/RemainTimeTool";
import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { RechargeData } from "./RechargeData";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { _millisec } from "../../../../../this/_millisec";
type int = number;
//class RechargeGiftBagPanel
    
    export  class RechargeGiftBagPanel /* flash.display.Sprite */
    {
        constructor(arg1: int, arg2: int)
        {
            
            this._itemArr = [];
            this._pageTool = new PageTool([], 10);
            this._width = arg1;
            this._height = arg2;
            this._career = Cache.instance.role.entityInfo.career;
            this.init();
            addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }

        /* internal  */onBagItemMouseClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._itemArr.length) 
            {
                loc1 = this._itemArr[loc2];
                loc1.setClickState(false);
                ++loc2;
            }
            if (typeof arg1.target === "rechargegiftbagitem") 
            {
                loc1 = arg1.target as RechargeGiftBagItem;
            }
            else 
            {
                loc1 = arg1.currentTarget as RechargeGiftBagItem;
            }
            if (loc1) 
            {
                this._valueTxt.text = loc1.value + "";
                this._giftBagTxt.htmlText = loc1.numStr + "<font color=\'#ff00ff\'>" + loc1.nameStr + "</font><font color=\'#b1efff\'>" + Language.getString(20142) + "</font>";
                this.updateRewardList(loc1.itemCode);
                if (loc1.code != 1) 
                {
                    this._tipsTxt.visible = true;
                    this._rechargeBtn.visible = true;
                    this._progressBar.visible = true;
                    this._noviceGiftTxt.visible = false;
                    if (loc1.code != 2) 
                    {
                        this._firstBagTxt.visible = false;
                        this._resTimeTxt.visible = true;
                    }
                    else 
                    {
                        this._firstBagTxt.visible = true;
                        this._resTimeTxt.visible = false;
                    }
                }
                else 
                {
                    this._tipsTxt.visible = false;
                    this._rechargeBtn.visible = false;
                    this._progressBar.visible = false;
                    this._noviceGiftTxt.visible = true;
                    this._resTimeTxt.visible = false;
                    this._firstBagTxt.visible = false;
                }
                loc1.setClickState(true);
                Dispatcher.dispatchEvent(new DataEvent(EventName.RechargeGiftBagPanelItemSelect, loc1.code));
            }
            return;
        }

        /* internal  */updateRewardList(arg1: int): void
        {
            var loc2=null;
            var loc4=null;
            this._pagebar.currentPage = 1;
            var loc1=GiftBagConfig.instance.getInfoByType(arg1);
            var loc3=[];
            var loc5=0;
            var loc6=loc1;
            for(loc2 of loc6) 
            {
                if (!(loc2.type == 3 && (loc2.career == 0 || (loc2.career & this._career) > 0))) 
                {
                    continue;
                }
                loc3.push(loc2);
            }
            this._pageTool.array = loc3;
            this._pagebar.maxPage = Math.ceil(loc3.length / 10);
            loc4 = this._pageTool.getByPageNo(1);
            this._tileList.dataProvider = new DataProvider(loc4);
            return;
        }

        /* internal  */onRechargeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GotoPay));
            return;
        }

        /* internal  */onAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.addEventListener(EventName.HavePay, this.onMoneyUpdateHandler);
            this.updateMoney();
            return;
        }

        /* internal  */onRemoveFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.removeEventListener(EventName.HavePay, this.onMoneyUpdateHandler);
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            this.updateMoney();
            return;
        }

        /* internal  */init(): void
        {
            var loc16=null;
            var loc17=0;
            var loc18=0;
            var loc19=null;
            var loc20=null;
            var loc21=null;
            var loc22=null;
            var loc23=null;
            var loc24=null;
            var loc25=null;
            var loc26=null;
            var loc27=null;
            ObjCreate.createBg(0, 0, this._width, this._height, this);
            var loc1=ObjCreate.createBitmap(ImagesConst.RechargeFlwBg, 38, 12, this);
            var loc2=ObjCreate.createBitmap(ImagesConst.RechargeFlwBg, 650, 12, this);
            loc2.rotationY = -180;
            var loc3=ObjCreate.createBitmap(ImagesConst.RechargeFlwBg, 130, 210, this);
            var loc4;
            (loc4 = ObjCreate.createBitmap(ImagesConst.RechargeFlwBg, 545, 210, this)).rotationY = -180;
            var loc5=ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 630, 2, this);
            var loc6=ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 630, 196, this);
            var loc7;
            (loc7 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 60, 2, this)).rotationY = 180;
            var loc8;
            (loc8 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 60, 196, this)).rotationY = 180;
            var loc9;
            (loc9 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 630, 310, this)).rotationX = -180;
            var loc10;
            (loc10 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 630, 395, this)).rotationX = -180;
            var loc11;
            (loc11 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 60, 310, this)).rotationX = -180;
            loc11.rotationY = 180;
            var loc12;
            (loc12 = ObjCreate.createBitmap(ImagesConst.RechargeSmallBg, 60, 395, this)).rotationX = -180;
            loc12.rotationY = 180;
            var loc13=new GImageBitmap(ImagesConst.RechargeDecs, 85, 7);
            this.addChild(loc13);
            var loc14=new GImageBitmap(ImagesConst.RechargeWorth, 560, 220);
            this.addChild(loc14);
            ObjCreate.createBg(15, 196, 660, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(15, 310, 660, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(33, 28, 590, 1, this, ImagesConst.KillUserLine);
            ObjCreate.createBg(125, 225, 405, 1, this, ImagesConst.KillUserLine);
            ObjCreate.createBg(85, 235, 500, 45, this, ImagesConst.RemainningTimeBg);
            var loc15=OpenRechargeGiftBagConfig.instance.getOpenRechargeGiftBagMap();
            loc17 = 10;
            loc18 = 35;
            var loc28=0;
            var loc29=loc15;
            for(loc19 of loc29) 
            {
                (loc16 = new RechargeGiftBagItem()).x = loc17;
                loc16.y = loc18;
                loc16.updateData(loc19);
                addChild(loc16);
                loc17 = loc17 + loc16.pwidth + 3;
                loc16.addEventListener(flash.events.MouseEvent.CLICK, this.onBagItemMouseClickHandler);
                this._itemArr.push(loc16);
            }
            loc20 = new GTextFormat(FontUtil.xingkaiName, 20, 16776960, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._giftBagTxt = ObjCreate.createTextField("", 150, 200, 390, 40, this, loc20);
            loc21 = new GTextFormat(FontUtil.EmbedNumberName, 30, 16776960);
            this._valueTxt = ObjCreate.createTextField("588", 620, 229, 100, 40, this, loc21);
            this._valueTxt.embedFonts = true;
            this._tileList = new GTileList();
            this._tileList.columnWidth = 38;
            this._tileList.rowHeight = 38;
            this._tileList.columnCount = 10;
            this._tileList.rowCount = 1;
            this._tileList.horizontalGap = 5;
            this._tileList.setStyle("skin", new Object /* flash.display.Bitmap */());
            this._tileList.setStyle("cellRenderer", NoviceGiftItemRenderer);
            this._tileList.setSize(430, 40);
            this._tileList.move(125, 239);
            this._tileList.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._tileList.verticalScrollPolicy = ScrollPolicy.OFF;
            addChild(this._tileList);
            this._pagebar = new PageSelecter();
            this._pagebar.mode = PageSelecter.SingleMode;
            this._pagebar.move(295, 286);
            this._pagebar.maxPage = 2;
            this._pagebar.currentPage = 1;
            this._pagebar.addEventListener(flash.events.Event.CHANGE, this.onCurPageChangeHandler);
            addChild(this._pagebar);
            loc22 = new GTextFormat("", 12, 11661311, true);
            this._tipsTxt = ObjCreate.createTextField("", 10, 320, 600, 20, this, loc22);
            this._progressBar = new RechargeProgressBar();
            this._progressBar.move(0, 340);
            this._progressBar.progressBarClass = ImagesConst.ProgressInner;
            this._progressBar.isHaveProgressPoint(true);
            this._progressBar.setValue(10, 100);
            addChild(this._progressBar);
            loc23 = new GTextFormat("", 14, 65280, true);
            this._resTimeTxt = ObjCreate.createTextField("", 200, 370, 400, 20, this, loc23);
            loc24 = new GTextFormat("", 12, 16776960, true);
            this._resMillisecTxt = ObjCreate.createTextField("", 460, 1, 300, 30, this, loc24);
            this._rechargeBtn = ObjCreate.createGLoadedButton(ResFileConst.OpenRecharge, 565, 330, 97, 50, this);
            this._rechargeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRechargeBtnClickHandler);
            this._secTimer = new SecTimer();
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrameHandler);
            this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            loc25 = Language.getString(20140);
            loc26 = new GTextFormat("", 12, 11661311, null, null, null, null, null, null, null, null, null, 3);
            this._noviceGiftTxt = ObjCreate.createTextField("", 40, 320, 500, 120, this, loc26);
            this._noviceGiftTxt.htmlText = loc25;
            this._noviceGiftTxt.visible = false;
            loc27 = Language.getString(20153);
            this._firstBagTxt = ObjCreate.createTextField("", 50, 370, 500, 20, this);
            this._firstBagTxt.htmlText = loc27;
            this._firstBagTxt.visible = false;
            return;
        }

        /* internal  */onCurPageChangeHandler(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=this._pageTool.getByPageNo(this._pagebar.currentPage);
            this._tileList.dataProvider = new DataProvider(loc1);
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer): void
        {
            this._resTimeTxt.htmlText = Language.getStringByParam(20141, RemainTimeUtil.getRemainTimeBySeconds(this._secTimer.repeatCount, true, false));
            this._milliStr = RemainTimeTool.getRemainTimeByMillisecond(this._secTimer.repeatCount, "#ff0000", "#ffff00", 22);
            this._resMillisecTxt.htmlText = this._milliStr;
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            return;
        }

        /* internal  */onEnterFrameHandler(arg1: FrameTimer): void
        {
            this._millisec = this._millisec + 3;
            if (this._millisec >= 99) 
            {
                this._millisec = 1;
            }
            this._resMillisecTxt.htmlText = this._milliStr + "<font color=\'#ff0000\' size=\'22\'>" + (this._millisec < 10 ? "0" + this._millisec : _millisec) + "</font>";
            return;
        }

        public updatePanelInfo(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._itemArr.length) 
            {
                loc1 = this._itemArr[loc2];
                loc1.setClickState(false);
                ++loc2;
            }
            this._firstGiftBagItem = this._itemArr[1] as RechargeGiftBagItem;
            this._firstGiftBagItem.setClickState(true);
            this._tipsTxt.visible = true;
            this._rechargeBtn.visible = true;
            this._progressBar.visible = true;
            this._noviceGiftTxt.visible = false;
            this._firstBagTxt.visible = true;
            this._resTimeTxt.visible = false;
            this._valueTxt.text = this._firstGiftBagItem.value + "";
            this._giftBagTxt.htmlText = this._firstGiftBagItem.numStr + "<font color=\'#ff00ff\'>" + this._firstGiftBagItem.nameStr + "</font><font color=\'#b1efff\'>" + Language.getString(20142) + "</font>";
            this.updateRewardList(this._firstGiftBagItem.itemCode);
            this.updateMoney();
            this.updateGiftBagInfo();
            return;
        }

        public updateGiftBagInfo(): void
        {
            var loc2=null;
            var loc5=0;
            var loc6=0;
            var loc1=RechargeData.hadGetCodes;
            if (loc1 && loc1.length > 0) 
            {
                loc5 = 0;
                while (loc5 < this._itemArr.length) 
                {
                    loc2 = this._itemArr[loc5];
                    loc6 = 0;
                    while (loc6 < loc1.length) 
                    {
                        if (loc2.code == loc1[loc6]) 
                        {
                            loc2.setGetBtn(false);
                        }
                        ++loc6;
                    }
                    loc2.updateHadGetState();
                    ++loc5;
                }
            }
            var loc3=0;
            while (loc3 < this._itemArr.length) 
            {
                loc2 = this._itemArr[loc3];
                loc2.setCanGetState();
                loc2.updateHadGetState();
                ++loc3;
            }
            var loc4=RechargeData.restTime;
            this._secTimer.reset();
            this._secTimer.repeatCount = loc4;
            if (loc4 > 0) 
            {
                this._secTimer.start();
                this._resTimeTxt.htmlText = Language.getStringByParam(20141, RemainTimeUtil.getRemainTimeBySeconds(loc4, true, false));
            }
            return;
        }

        public updateMoney(): void
        {
            var loc2=0;
            var loc3=0;
            var loc4=null;
            var loc1=Cache.instance.role.recharge;
            var loc5=OpenRechargeGiftBagConfig.instance.getRechargeTargetArr();
            var loc6=0;
            while (loc6 < loc5.length) 
            {
                if (loc5[loc6] > loc1) 
                {
                    loc4 = OpenRechargeGiftBagConfig.instance.getGiftBagInfoByTarget(loc5[loc6], 1, Cache.instance.role.activeWorldLevel);
                    break;
                }
                ++loc6;
            }
            if (loc4) 
            {
                loc2 = loc4.target - loc1;
                loc3 = loc4.target;
                this._progressBar.setValue(loc1, loc3);
            }
            else 
            {
                this._progressBar.setValue(20000, 20000);
            }
            var loc7=OpenRechargeGiftBagConfig.instance.getFinalInfoByCode(7, 1);
            var loc8="";
            var loc9=588;
            if (this._firstGiftBagItem) 
            {
                loc9 = this._firstGiftBagItem.value;
            }
            if (loc1 <= 0) 
            {
                loc8 = Language.getStringByParam(20143, loc9);
            }
            else if (loc7 && loc1 >= loc7.target) 
            {
                loc8 = Language.getString(20144);
            }
            else 
            {
                loc8 = Language.getStringByParam(20145, loc1, loc2, loc3);
            }
            this._tipsTxt.htmlText = loc8;
            return;
        }

        /* internal */ /* var */_width: int;

        /* internal */ /* var */_height: int;

        /* internal */ /* var */_giftBagTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tileList: GTileList;

        /* internal */ /* var */_valueTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tipsTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_resTimeTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_resMillisecTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_progressBar: RechargeProgressBar;

        /* internal */ /* var */_itemArr: Array<any>;

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_pagebar: PageSelecter;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_rechargeBtn: GLoadedButton;

        /* internal */ /* var */_noviceGiftTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_career: int;

        /* internal */ /* var */_firstGiftBagItem: RechargeGiftBagItem;

        /* internal */ /* var */_millisec: int=0;

        /* internal */ /* var */_milliStr: string;

        /* internal */ /* var */_firstBagTxt: Object /* flash.text.TextField */;
    }
